﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace YJKArch.EditFloor
{
	public partial class EditFloorThicknessForm : System.Windows.Forms.Form
	{
		public EditFloorThicknessForm(string refFloorTypeName, List<FloorThicknessInfos> floorThicknessInfos, List<string> allFloorTypeNames)
		{
			this.InitializeComponent();
			this.m_AllFloorTypeNames = allFloorTypeNames;
			this.m_FloorThicknessInfos = floorThicknessInfos;
			this.InitDGV();
			this.tbTypeName.Text = "HY_" + refFloorTypeName;
		}

		public string NewFloorTypeName
		{
			get
			{
				return this.tbTypeName.Text;
			}
		}

		public List<FloorThicknessInfos> FloorThicknessInfos
		{
			get
			{
				return this.m_FloorThicknessInfos;
			}
		}

		private void DGVFloorThickness_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
		{
			System.Windows.Forms.TextBox textBox = e.Control as System.Windows.Forms.TextBox;
			if (textBox != null)
			{
				textBox.TextChanged += this.DGVTextBoxTextChanged;
			}
		}

		private void InitDGV()
		{
			this.DGVFloorThickness.Rows.Clear();
			if (this.m_FloorThicknessInfos.Count > 0)
			{
				this.DGVFloorThickness.RowCount = this.m_FloorThicknessInfos.Count;
				for (int i = 0; i < this.m_FloorThicknessInfos.Count; i++)
				{
					this.DGVFloorThickness.Rows[i].Cells[0].Value = this.m_FloorThicknessInfos[i].m_Name;
					double width = this.m_FloorThicknessInfos[i].m_Width;
					this.DGVFloorThickness.Rows[i].Cells[1].Value = width.ToString();
				}
			}
		}

		private void DGVTextBoxTextChanged(object sender, EventArgs e)
		{
			int num = 1;
			if (this.DGVFloorThickness.CurrentCell.ColumnIndex == num)
			{
				int rowIndex = this.DGVFloorThickness.CurrentCell.RowIndex;
				string text = ((System.Windows.Forms.TextBox)sender).Text;
				int num2 = 0;
				try
				{
					num2 = Convert.ToInt32(text);
				}
				catch (Exception)
				{
					((System.Windows.Forms.TextBox)sender).Text = this.m_FloorThicknessInfos[rowIndex].m_Width.ToString();
					return;
				}
				if (num2 < 0)
				{
					((System.Windows.Forms.TextBox)sender).Text = this.m_FloorThicknessInfos[rowIndex].m_Width.ToString();
					MessageBox.Show("宽度不能小于零", "盈建科提示");
					return;
				}
				this.m_FloorThicknessInfos[rowIndex] = new FloorThicknessInfos(this.m_FloorThicknessInfos[rowIndex].m_Funtion, this.m_FloorThicknessInfos[rowIndex].m_Name, (double)num2);
			}
		}

		private bool HasSpecialChar(string str)
		{
			char[] separator = new char[]
			{
				'/',
				'\\',
				':',
				'*',
				'?',
				'"',
				'<',
				'>',
				'|'
			};
			return str.Split(separator).Length != 1;
		}

		private void EditFloorThicknessForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (base.DialogResult == DialogResult.Cancel)
			{
				return;
			}
			string newTypeName = this.tbTypeName.Text;
			if (newTypeName.CompareTo("") == 0)
			{
				MessageBox.Show("楼板类型名不能为空", "盈建科提示");
				e.Cancel = true;
			}
			if (this.m_AllFloorTypeNames.FindIndex((string t) => t.CompareTo(newTypeName) == 0) >= 0)
			{
				MessageBox.Show("楼板类型名已存在", "盈建科提示");
				e.Cancel = true;
			}
			if (this.HasSpecialChar(newTypeName))
			{
				MessageBox.Show("楼板类型名含有非法字符", "盈建科提示");
				e.Cancel = true;
			}
			foreach (FloorThicknessInfos floorThicknessInfos in this.m_FloorThicknessInfos)
			{
				if (floorThicknessInfos.m_Name.CompareTo("涂膜层") == 0 && !this.IsEqual(floorThicknessInfos.m_Width, 0.0))
				{
					MessageBox.Show("\"涂膜层\"功能要求厚度为零", "盈建科提示");
					e.Cancel = true;
					break;
				}
			}
		}

		private bool LessThan(double val1, double val2)
		{
			return val1 - val2 < -1E-09;
		}

		private bool IsEqual(double val1, double val2)
		{
			return !this.LessThan(val1, val2) && !this.LessThan(val2, val1);
		}

		private List<string> m_AllFloorTypeNames;

		private List<FloorThicknessInfos> m_FloorThicknessInfos;
	}
}
